<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                  <el-form-item label="open_id" prop="openId">
                    <el-input
                        v-model="queryParams.openId"
                        placeholder="请输入open_id"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="昵称" prop="nickName">
                    <el-input
                        v-model="queryParams.nickName"
                        placeholder="请输入昵称"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="年级" prop="grade">
                    <el-input
                        v-model="queryParams.grade"
                        placeholder="请输入年级"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="教材" prop="textbook">
                    <el-input
                        v-model="queryParams.textbook"
                        placeholder="请输入教材"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="我的id" prop="myId">
                    <el-input
                        v-model="queryParams.myId"
                        placeholder="请输入我的id"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="appUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="id" align="center" prop="id" />
              <el-table-column label="手机号" align="center" prop="phone" />
              <el-table-column label="open_id" align="center" prop="openId" />
      <el-table-column label="头像" align="center" prop="headImg">
        <template slot-scope="scope">
          <image-preview :src="scope.row.headImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
              <el-table-column label="昵称" align="center" prop="nickName" />
              <el-table-column label="年级" align="center" prop="grade" />
              <el-table-column label="用户类型 " align="center" prop="type" />
              <el-table-column label="教材" align="center" prop="textbook" />
              <el-table-column label="我的id" align="center" prop="myId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="emitSelect(scope.row)"
          >选中</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

  </div>
</template>

<script>
  import { listAppUser, getAppUser, delAppUser, addAppUser, updateAppUser } from "@/api/system/appUser";

  export default {
    name: "AppUser",
        dicts: ['sys_common_status', 'is_not', 'sys_user_sex'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // app用户管理表格数据
              appUserList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
                        openId: null,
                        nickName: null,
                        status: 0,
                        grade: null,
                        type: null,
                        textbook: null,
                        myId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
                        phone: [
                    { required: true, message: "手机号不能为空", trigger: "blur" }
                  ],
                        headImg: [
                    { required: true, message: "头像不能为空", trigger: "blur" }
                  ],
                        nickName: [
                    { required: true, message: "昵称不能为空", trigger: "blur" }
                  ],
                        status: [
                    { required: true, message: "状态  0 否 1 是不能为空", trigger: "change" }
                  ],
                        grade: [
                    { required: true, message: "年级不能为空", trigger: "blur" }
                  ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      emitSelect(row) {
        this.$emit("appUserData", row);
      },
      /** 查询app用户管理列表 */
      getList() {
        this.loading = true;
        listAppUser(this.queryParams).then(response => {
          this.appUserList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
                        id: null,
                        phone: null,
                        password: null,
                        openId: null,
                        headImg: null,
                        nickName: null,
                        birthday: null,
                        email: null,
                        status: null,
                        sex: null,
                        age: null,
                        grade: null,
                        invitationCode: null,
                        type: null,
                        textbook: null,
                        points: null,
                        myId: null,
                        delFlag: null,
                        loginIp: null,
                        loginDate: null,
                        createBy: null,
                        createTime: null,
                        updateBy: null,
                        updateTime: null,
                        remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加app用户管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getAppUser(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改app用户管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateAppUser(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addAppUser(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除app用户管理编号为"' + ids + '"的数据项？').then(function() {
          return delAppUser(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
  /** 导出按钮操作 */
  handleExport() {
    this.download('system/appUser/export', {
      ...this.queryParams
    }, `appUser_${new Date().getTime()}.xlsx`)
  }
  }
  };
</script>
